package cn.sumpu.app.wlcity.web.mvc.light;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sumpu.app.wlcity.biz.VoteBiz;
import cn.sumpu.app.wlcity.domain.Vote;
import cn.sumpu.app.wlcity.domain.VoteItem;
import cn.sumpu.app.wlcity.exception.AppBusinessException;
import cn.sumpu.app.wlcity.util.Common;

@Controller
public class LightVoteController {
	@Autowired
	private VoteBiz voteBiz;
	

	@RequestMapping(value = "/lightVote.htm")
	public String lightVote(HttpServletRequest request, Integer page,String app) {
		try {
			if (page == null)
				page = 1;
			app=Common.APP_LIGHT;
			List<Vote> voteList = voteBiz.getFrontVoteList(page,app);
			request.setAttribute("voteList", voteList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
			return "hotspring/error";
		}
		return "light/vote/vote";
	}
	//有奖投票
	@RequestMapping(value = "/getLightVoteItemList.htm")
	public String getLightVoteItemList(HttpServletRequest request, String voteId,Integer page) {
		try {
			List<VoteItem> voteItemList = voteBiz.getVoteItemList(voteId,page);  
			int count=voteBiz.getVoteItemListCount(voteId);
			int totalPage=(count + Common.PAGE_SIZE_VOTE_ITEM - 1)/ Common.PAGE_SIZE_VOTE_ITEM; 
			Vote vote=voteBiz.getVoteById(voteId);
			if(Common.empty(page))
				page=1;
			request.setAttribute("voteItemList", voteItemList);
			request.setAttribute("voteTitle", vote.getTitle());
			request.setAttribute("voteId", voteId);
			request.setAttribute("totalPage", totalPage);
			request.setAttribute("currentPage", page);
			request.setAttribute("link", "getLightVoteItemList.htm?voteId="+voteId+"&page=");
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "light/vote/voteItemList";
	}
	//投一票
	@RequestMapping(value = "/getLightVoteItem.htm")
	public String getVoteItemById(HttpServletRequest request, String voteItemId,String voteItemTitle) {
		try {
			VoteItem voteItem = voteBiz.getVoteItemById(voteItemId);
			String voteId=voteItem.getVoteId();
			Vote vote=voteBiz.getVoteById(voteId);
			request.setAttribute("voteId", voteItem.getVoteId());
			request.setAttribute("voteTitle", vote.getTitle());
			request.setAttribute("voteItem", voteItem);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "light/vote/voteItemDetail";
	}
	
	
}
